//
//  UIViewController+choosePhoto.m
//  cascade-realm
//
//  Created by QSC on 28/5/24.
//

#import "UIViewController+choosePhoto.h"
#import <Photos/Photos.h>
@interface UIViewController()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic,copy)void(^SelectedImageBlock)(UIImage *image,NSData * imageData);
@end
@implementation UIViewController (choosePhoto)
-(void)getImageWithSelectedImageBlock:(void(^)(UIImage *image,NSData * imageData))SelectedImageBlock{
    
   
    self.SelectedImageBlock = SelectedImageBlock;
    
    
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        
          
        if (PHAuthorizationStatusDenied != status) {
            //打开相册
            dispatch_async(dispatch_get_main_queue(), ^{
                [self showAlertImagPicker];
            });
         
            
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                [EasyTextView showText:@"Unable to access album, please grant album permission"];
            });
        }
        
    }];
    
   
}


static const char SelectedImageBlockKey = '\0';
- (void)setSelectedImageBlock:(void (^)(UIImage *,NSData *))SelectedImageBlock{
    objc_setAssociatedObject(self, &SelectedImageBlockKey, SelectedImageBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void (^)(UIImage *,NSData *))SelectedImageBlock{
    return  objc_getAssociatedObject(self, & SelectedImageBlockKey);
}


- (void)showAlertImagPicker{
    
  
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    //取消:style:UIAlertActionStyleCancel
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *moreAction = [UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        [self openImagePick:UIImagePickerControllerSourceTypeCamera];
    }];
    UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"Photo Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        [self openImagePick:UIImagePickerControllerSourceTypePhotoLibrary];
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:moreAction];
    [alertController addAction:OKAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
 
}


- (void)openImagePick:(UIImagePickerControllerSourceType)type
{
    if ([UIImagePickerController isSourceTypeAvailable:type]) {
        UIImagePickerController *imagePickerVc = [[UIImagePickerController alloc]init];
        imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen;
        imagePickerVc.sourceType = type;
        imagePickerVc.delegate = self;
        [self presentViewController:imagePickerVc animated:YES completion:nil];
    }else{
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"CascadeRealm requires the photo album to upload work pictures or personalized avatars" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *actionCacel = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil];
        [alertController addAction:actionCacel];
        [self presentViewController:alertController animated:YES completion:nil];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    
    UIImage * OriginalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    //先压缩一下
    NSData *data = UIImageJPEGRepresentation(OriginalImage, 0.3);;
    UIImage *image = [UIImage imageWithData:data];
    
    if (self.SelectedImageBlock) {
        
        self.SelectedImageBlock(image,data);
    }
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}




@end
